od = {};

od.submit = function(){
	var data = jQuery('#UserOrderAddForm').serializeObject();
	if(!od.check(data)) return;
	
	var callback = function(res){
		if(res.state){
			location.href = res.data.redirect;
		}else{
			zmx.tip(res.msg,false);
			zmx.shineTip();
		}
	};
	ad.ajax(data,callback,'UserOrders/add');
};

od.gettype = function(){
	jQuery('#ps input').click(function(){
		var sg = jQuery(this).val()
		if(sg == 1){				//发货
			jQuery('#js').show();
			jQuery('#smqh').hide();
			od.logistic();
		}else{						//自取
			jQuery('#smqh').show();
			jQuery('#js').hide();
			od.sumallprice(0);
		};
	});
	od.logistic();
	od.switchaddress();
};

od.sumallprice = function(logistic){
	var allprice = jQuery('#order_ticket_price').data('price');
	jQuery('#logistic').html(logistic);
	if(jQuery('#UserOrderAddForm .pluslogistic').length<=0){
		jQuery('#delogistic').html(logistic);
		jQuery('#all_price').html(Number(allprice));
	}else{
		jQuery('#all_price').html(Number(allprice)+Number(logistic));
	}
}

od.logistic  = function(){
	jQuery('#logistic').html('loading');
	var address = jQuery('input[name="data[UserOrder][address_id]"]:checked').val();
	if(address == 0){
		od.sumallprice(0);
	}else{
		od.getlogistic(address);
	}
};

od.getlogistic = function(id){
	var callback = function(res){
		if(res.state){
			od.sumallprice(res.data);
		}
	};
	ad.ajax({},callback,'SysLogisticTpls/charge/'+id);
};

od.switchaddress = function(){
	jQuery('#u_a_l').on('click','input[name="data[UserOrder][address_id]"]',function(){
		var address = jQuery('input[name="data[UserOrder][address_id]"]:checked').val();
		if(address == 0){
			od.sumallprice(0);
		}else{
			od.getlogistic(address);
		}
		jQuery('#new_address').hide();
	});
};

od.check = function(data){
	if(data['data[UserOrder][sys_gettype_id]'] == 1 
		&& data['data[UserOrder][address_id]']<=0){
		zmx.tip('请选择收货地址',false);
		zmx.shineTip();
		return false;		
	}else if(data['data[UserOrder][sys_gettype_id]'] == 2){
		if(data['data[UserOrder][address_receive]']==''){
			zmx.tip('请输入联系人',false);
			zmx.shineTip();
			return false;
		}
		if(data['data[UserOrder][address_phone]']==''){
			zmx.tip('请输入联系电话',false);
			zmx.shineTip();
			return false;
		}
	}
	
	return true;
};
od.ajax = function(data,callback,action){
	$.ajax({
	  type: "post",
	  url: action,
	  dataType:'json',
	  data:data,
	}).done(function( msg ) {
		callback(msg);
	});
}
od.paytypeEvent = function(){
	var $tar = jQuery('#pay-type-select');
	$tar.find('.handel:first').addClass('active');
	$tar.find('.handel').click(function(){
		$tar.find('.handel').removeClass('active');
		$(this).addClass('active');
		var sid = jQuery(this).data('id');
		jQuery('#UserOrderSysPaytypeId').val(sid);
		$tar.find('.ptc').hide();
		$tar.find('.uop-'+sid).show().find('input').eq(0).click();	
	});
};

od.needtitle = function(){
	jQuery('#need_title').change(function(){
		if($(this).is(':checked')){
			jQuery('#needtitle').show();
		}else{
			jQuery('#needtitle').hide();
		}
	});
};
